package cn.codergege.demo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.AbstractWebSocketHandler;

public class StimulateProgressHandler extends AbstractWebSocketHandler {
    private final Logger log = LoggerFactory.getLogger(StimulateProgressHandler.class);

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        log.info("Recieved message: " + message.getPayload());

        Thread.sleep(2000); // 模拟延迟
        // 获取 控制台输出

        session.sendMessage(new TextMessage("page:" + Math.random()*10 + "-total:25"));
    }

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        log.info("Connection established!");
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        log.info("Connection closed!");
    }
}
